package com.woniu.user.service.impl;

import com.woniu.entity.Result;
import com.woniu.user.mapper.UserMapper;
import com.woniu.user.pojo.User;
import com.woniu.user.pojo.UserCircle;
import com.woniu.user.mapper.UserCircleMapper;
import com.woniu.user.pojo.UserLikeCar;
import com.woniu.user.service.IUserCircleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 我的车圈表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@Service
public class UserCircleServiceImpl extends ServiceImpl<UserCircleMapper, UserCircle> implements IUserCircleService {

    @Autowired(required = false)
    private UserCircleMapper userCircleMapper;
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public Result addLikeCircle(String userPhone, String circleId) {
        User user = userMapper.selectByPhone(userPhone);

        UserCircle userCircle = userCircleMapper.selectIsLikeCircle(user.getId(), circleId);
        if (userCircle == null) {
            UserCircle userCircle1 = new UserCircle();
            userCircle1.setUserId(user.getId());
            userCircle1.setCarCircleId(circleId);
            int insert = userCircleMapper.insert(userCircle1);
            if (insert > 0) {
                return Result.ok(200, "关注成功");
            }
        }

        if (userCircle.getIsDeleted() == 0) {
            return Result.error(200, "你已经关注过了");
        }

        if (userCircle.getIsDeleted() == 1) {
            int update = userCircleMapper.updateIsDeleted(user.getId(), circleId);
            if (update > 0) {
                return Result.ok(200, "关注成功!");
            }
            return Result.error(500);
        }
        return Result.error(500);
    }

    @Override
    public Result rmLikeCircle(String userPhone, String circleId) {
        User user = userMapper.selectByPhone(userPhone);

        int rmLikeCar = userCircleMapper.rmLikeCircle(user.getId(), circleId);
        if (rmLikeCar > 0){
            return Result.ok(200,"已取消");
        }
        return Result.error(500);
    }
}
